package com.itzxx.huazhun.ui.device.add

import android.annotation.SuppressLint
import android.text.TextUtils
import com.itzxx.huazhun.data.model.DeviceType
import com.itzxx.huazhun.data.model.EventMessage
import com.itzxx.huazhun.data.model.EventType
import com.itzxx.huazhun.data.repository.HttpRequestCoroutine
import com.itzxx.library_base_ktx.base.viewmodel.BaseViewModel
import com.itzxx.library_base_ktx.callback.databind.StringObservableField
import com.itzxx.library_base_ktx.callback.livedata.event.EventLiveData
import com.itzxx.library_base_ktx.ext.request
import com.itzxx.library_base_ktx.view.BaseDialog
import com.itzxx.library_base_ktx.view.DateDialog
import com.itzxx.library_base_ktx.view.MenuDialog
import org.greenrobot.eventbus.EventBus
import java.util.TreeMap


class DeviceAddViewModel : BaseViewModel() {

    //设备名称
    val tvDevicesNameField = StringObservableField()

    //mac地址
    val tvDevicesMACField = StringObservableField()

    //设备类型
    val tvDevicesTypeField = StringObservableField()

    //设备品牌
    val tvBrandNameField = StringObservableField()

    //设备归属
    val tvAscriptionField = StringObservableField()

    //设备管理员
    val tvAdministratorsField = StringObservableField()

    //设备编号
    val tvNumberField = StringObservableField()

    //设备容量
    val tvCapacityField = StringObservableField()

    //设备序列号
    val tvSerialNumberField = StringObservableField()

    //设备生产日期
    val tvProductionDateField = StringObservableField()

    //设备安装日期
    val tvInstallationDateField = StringObservableField()

    val keepLiveData = EventLiveData<Boolean>()

    var typeList = arrayListOf<DeviceType>()


    fun productionDateClick() {
        baseContext.value?.let {  // 日期选择对话框
            DateDialog.Builder(it)
                .setListener(object : DateDialog.OnListener {

                    @SuppressLint("DefaultLocale")
                    override fun onSelected(dialog: BaseDialog?, year: Int, month: Int, day: Int) {
                        tvProductionDateField.set("$year-${String.format("%02d", month)}-${String.format("%02d", day)}")
                    }

                    override fun onCancel(dialog: BaseDialog?) {

                    }
                })
                .show() }
    }

    fun installDateClick() {
        baseContext.value?.let {  // 日期选择对话框
            DateDialog.Builder(it)
                .setListener(object : DateDialog.OnListener {

                    @SuppressLint("DefaultLocale")
                    override fun onSelected(dialog: BaseDialog?, year: Int, month: Int, day: Int) {

                        tvInstallationDateField.set("$year-${String.format("%02d", month)}-${String.format("%02d", day)}")
                    }

                    override fun onCancel(dialog: BaseDialog?) {

                    }
                })
                .show() }
    }

    fun typeClick() {
        val typeStrList = arrayListOf<String>()
        typeList.forEach {
            typeStrList.add(it.typeName)
        }
        baseContext.value?.let {  // 日期选择对话框
            MenuDialog.Builder(it)
                // 设置 null 表示不显示取消按钮
                //.setCancel(getString(R.string.common_cancel))
                // 设置点击按钮后不关闭对话框
                //.setAutoDismiss(false)
                .setList(typeStrList)
                .setListener(object : MenuDialog.OnListener<String> {
                    override fun onSelected(dialog: BaseDialog?, position: Int, data: String) {
                        tvDevicesTypeField.set(data)
                    }
                    override fun onCancel(dialog: BaseDialog?) {}
                })
                .show()
        }
    }

    fun keepClick() {
        when {
            TextUtils.isEmpty(tvDevicesNameField.get()) -> {
                    toast("请输入设备名称")
            }
            TextUtils.isEmpty(tvDevicesTypeField.get()) -> {
                    toast("请选择设备类型")
            }
//            TextUtils.isEmpty(tvDevicesMACField.get()) -> {
//                    toast("请输入MAC地址")
//            }
            TextUtils.isEmpty(tvBrandNameField.get()) -> {
                    toast("请输入设备品牌")
            }
            TextUtils.isEmpty(tvAscriptionField.get()) -> {
                toast("请输入设备归属")
            }
            TextUtils.isEmpty(tvAdministratorsField.get()) -> {
                toast("请输入设备管理员")
            }
            TextUtils.isEmpty(tvNumberField.get()) -> {
                toast("请输入设备编号")
            }
            TextUtils.isEmpty(tvCapacityField.get()) -> {
                toast("请输入设备容量")
            }
            TextUtils.isEmpty(tvSerialNumberField.get()) -> {
                toast("请输入设备号")
            }
            TextUtils.isEmpty(tvProductionDateField.get()) -> {
                toast("请选择生产日期")
            }
            TextUtils.isEmpty(tvInstallationDateField.get()) -> {
                toast("请选择安装日期")
            }
            else -> {
                loadingChange.showDialog.postValue("创建中...")
                val empty = TreeMap<String, Any>()
                empty.put("equipmentName",tvDevicesNameField.get())
                typeList.forEach {
                    if(TextUtils.equals(it.typeName,tvDevicesTypeField.get())){
                        empty.put("equipmentType",it.id)
                    }
                }
                empty.put("equipmentMac",tvDevicesMACField.get())
                empty.put("equipmentBrand",tvBrandNameField.get())
                empty.put("equipmentOwner",tvAscriptionField.get())
                empty.put("equipmentAdmin",tvAdministratorsField.get())
                empty.put("equipmentNo",tvNumberField.get())
                empty.put("equipmentCapacity",tvCapacityField.get())
                empty.put("equipmentSerialNo",tvSerialNumberField.get())
                empty.put("equipmentManufactureDate",tvProductionDateField.get())
                empty.put("equipmentInstallDate",tvInstallationDateField.get())
                request({
                    HttpRequestCoroutine.addDetectionEquipment(
                        empty
                    )
                }, {
                    EventBus.getDefault().post(EventMessage(EventType.ADD_DEVICES,""))
                    toast("创建成功")
                    loadingChange.dismissDialog.postValue(false)
                    keepLiveData.postValue(true)
                    finish()
                }, {
                    loadingChange.dismissDialog.postValue(false)
                    toast(it.errorMsg)
                    keepLiveData.postValue(false)
                })
            }
        }
    }

    fun getDevicesType(){
        request({
            HttpRequestCoroutine.getEquipmentType()
        }, {
            typeList = it
        }, {
            toast(it.errorMsg)
        })
    }

    override fun onCreateViewModel() {
        super.onCreateViewModel()
        getDevicesType()
    }
}